package com.simon.api;

import com.google.gson.Gson;
import com.simon.po.ProductInfo;
import redis.clients.jedis.Jedis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by changf on 2017/7/5.
 */
public class TestRedisSetString {

    public static void main(String[] args)throws Exception {

        Jedis redis=new Jedis("192.168.112.12",6379);


        ProductInfo info=new ProductInfo();

        info.setCategoryId(11);
        info.setName("门票");
        info.setPrice("114");


        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(info);

        byte[] bytes = byteArrayOutputStream.toByteArray();


        redis.set("product:01".getBytes(),bytes);
        System.out.println("======set bytes to redis");
        System.out.println("product:01".getBytes());
        byte[] bytes1 = redis.get("product:01".getBytes());

        //反序列化出来
        ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes1);
        ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
        ProductInfo o = (ProductInfo) objectInputStream.readObject();
        System.out.println("=====get data"+o);



        Gson gson=new Gson();
        String toJson = gson.toJson(info);
        redis.set("product:02",toJson);

        String s = redis.get("product:02");

        ProductInfo productInfo = gson.fromJson(s, ProductInfo.class);
        System.out.println("======from json get data"+productInfo);
    }
}
